<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="教师名称" prop="teacherName">
        <el-input
          v-model="queryParams.teacherName"
          placeholder="请输入教师名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="手机号" prop="teacherPhone">
        <el-input
          v-model="queryParams.teacherPhone"
          placeholder="请输入手机号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['edu:teacher:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate" v-hasPermi="['edu:teacher:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport" v-hasPermi="['edu:teacher:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="infoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="教师名称" align="center" prop="teacherName"/>
      <el-table-column label="毕业学校" align="center" prop="school"/>
      <el-table-column label="性别" align="center" prop="gender">
        <template v-slot:default="scope">
          <dict-tag :options="dict.type.sys_user_sex" :value="scope.row.gender"/>
        </template>
      </el-table-column>
      <el-table-column label="手机号" align="center" prop="teacherPhone"/>
      <el-table-column label="入职时间" align="center" prop="employmentDate" width="180">
        <template v-slot:default="scope">
          <span>{{ parseTime(scope.row.employmentDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="是否在职" align="center" prop="isEmployment">
        <template v-slot:default="scope">
          <dict-tag :options="dict.type.sys_yes_no" :value="scope.row.isEmployment"/>
        </template>
      </el-table-column>
      <el-table-column label="离职时间" align="center" prop="resignationTime" width="180">
        <template v-slot:default="scope">
          <span>{{ parseTime(scope.row.resignationTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template v-slot:default="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" v-hasPermi="['edu:teacher:edit']">修改</el-button>
<!--          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['edu:teacher:remove']">删除</el-button>-->
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleTermination(scope.row)" >离职</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改教师对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1000px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="教师名称" prop="teacherName">
              <el-input v-model="form.teacherName" placeholder="请输入教师名称"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">

            <el-form-item label="手机号" prop="teacherPhone">
              <el-input v-model="form.teacherPhone" placeholder="请输入手机号"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="性别" prop="gender">
              <el-select v-model="form.gender" placeholder="请选择性别" style="width: 100%">
                <el-option v-for="dict in dict.type.sys_user_sex" :key="dict.value" :label="dict.label"
                           :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="身份证号" prop="idCard">
              <el-input v-model="form.idCard" placeholder="请输入身份证号"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="入职时间" prop="employmentDate">
              <el-date-picker clearable style="width: 100%"
                              v-model="form.employmentDate"
                              type="date"
                              value-format="yyyy-MM-dd"
                              placeholder="请选择入职时间"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="毕业学校" prop="school">
              <el-input v-model="form.school" placeholder="请输入毕业学校"/>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="毕业时间" prop="graduationTime">
              <el-date-picker clearable style="width: 100%"
                              v-model="form.graduationTime"
                              type="date"
                              value-format="yyyy-MM-dd"
                              placeholder="请选择毕业时间"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="毕业专业" prop="major">
              <el-input v-model="form.major" placeholder="请输入毕业专业"/>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="24">
            <el-form-item label="荣誉简介" prop="remark">
              <el-input v-model="form.remark" type="textarea" placeholder="请输入内容"/>
            </el-form-item>

          </el-col>
        </el-row>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <el-dialog title="教师离职" :visible.sync="openTermination" width="1000px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="离职时间" prop="resignationTime">
              <el-date-picker clearable style="width: 100%" v-model="form.resignationTime"
                              type="date" value-format="yyyy-MM-dd" placeholder="请选择离职时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listInfo, getTeacherInfo, delTeacherInfo, addTeacherInfo, updateTeacherInfo } from '@/api/edu/teacher'

export default {
  name: 'Teacher',
  dicts: ['sys_user_sex','sys_yes_no'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 教师表格数据
      infoList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      openTermination: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        teacherName: null,
        school: null,
        graduationTime: null,
        major: null,
        gender: null,
        teacherPhone: null,
        idCard: null,
        employmentDate: null,
        avatar: null,
        isEmployment: null,
        resignationTime: null,
        tenantId: null,
        tenantName: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        teacherName: [
          { required: true, message: "教师名称不能为空", trigger: "blur" }
        ],
        teacherPhone: [
          { required: true, message: "手机号不能为空", trigger: "blur" }
        ],
        employmentDate: [
          { required: true, message: "请选择入职时间", trigger: "blur" }
        ],
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询教师列表 */
    getList() {
      this.loading = true
      listInfo(this.queryParams).then(response => {
        this.infoList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        teacherName: null,
        school: null,
        graduationTime: null,
        remark: null,
        major: null,
        gender: null,
        teacherPhone: null,
        idCard: null,
        employmentDate: null,
        avatar: null,
        isEmployment: null,
        resignationTime: null,
        tenantId: null,
        tenantName: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加教师'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getTeacherInfo(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = '修改教师'
      })
    },
    /** 离职按钮操作 */
    handleTermination(row) {
      this.reset()
      const id = row.id || this.ids
      getTeacherInfo(id).then(response => {
        this.form = response.data
        this.openTermination = true
        this.form.isEmployment = "N"
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateTeacherInfo(this.form).then(response => {
              this.$modal.msgSuccess('修改成功')
              this.open = false
              this.openTermination = false
              this.getList()
            })
          } else {
            addTeacherInfo(this.form).then(response => {
              this.$modal.msgSuccess('新增成功')
              this.open = false
              this.openTermination = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除教师编号为"' + ids + '"的数据项？').then(function() {
        return delTeacherInfo(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('删除成功')
      }).catch(() => {
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('edu/info/export', {
        ...this.queryParams
      }, `info_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>
